/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lu.med.praxiswahl.wahlumed.dao.beans;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import lu.med.praxiswahl.wahlumed.dao.beans.daointerface.DeliveriesDAOInt;
import lu.med.praxiswahl.wahlumed.model.beans.DeliveriesTBL;

/**
 *
 * @author pkendzo
 */

@Stateless
public class DeliveriesDAO implements DeliveriesDAOInt{

	@PersistenceContext(unitName = "gynaekomedic-ejbPU")
	private EntityManager	em	= null;
	
	public DeliveriesDAO()
	{
		
	}
	
	
	@Override
	public void delete(Long todo)
	{
		DeliveriesTBL del = em.find(DeliveriesTBL.class, todo);
        em.remove(del);
        em.flush();
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DeliveriesTBL> findAvailableDeliveries()
	{
		try
		{
			return (List<DeliveriesTBL>) em.createQuery(
					"select d from DeliveriesTBL d order by d.artikel").getResultList();
		}
		catch (Exception e)
		{
			throw new RuntimeException("DeliveriesTBL is empty", e);
		}
	}

	
	@Override
	public void persist(DeliveriesTBL todo)
	{
		em.persist(todo);
		em.flush();
		
	}

	@Override
	public void update(DeliveriesTBL todo)
	{
		em.merge(todo);
		em.flush();
		
	}


	@Override
	public List<DeliveriesTBL> sortByItem() {
		return this.findAvailableDeliveries();
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<DeliveriesTBL> sortByLieferant() {
		try
		{
			return (List<DeliveriesTBL>) em.createQuery(
					"select d from DeliveriesTBL d order by d.lieferant_name").getResultList();
		}
		catch (Exception e)
		{
			throw new RuntimeException("DeliveriesTBL is empty", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DeliveriesTBL> sortByPrice() {
		try
		{
			return (List<DeliveriesTBL>) em.createQuery(
					"select d from DeliveriesTBL d order by d.preis").getResultList();
		}
		catch (Exception e)
		{
			throw new RuntimeException("DeliveriesTBL is empty", e);
		}
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<DeliveriesTBL> sortByPurchaseDate() {
		try
		{
			return (List<DeliveriesTBL>) em.createQuery(
					"select d from DeliveriesTBL d order by d.bestell_datum asc").getResultList();
		}
		catch (Exception e)
		{
			throw new RuntimeException("DeliveriesTBL is empty", e);
		}
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<DeliveriesTBL> sortByBestand() {
		try
		{
			return (List<DeliveriesTBL>) em.createQuery(
					"select d from DeliveriesTBL d order by d.bestand").getResultList();
		}
		catch (Exception e)
		{
			throw new RuntimeException("DeliveriesTBL is empty", e);
		}
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<DeliveriesTBL> sortByLiefertDatum() {
		try
		{
			return (List<DeliveriesTBL>) em.createQuery(
					"select d from DeliveriesTBL d order by d.eingang_datum asc").getResultList();
		}
		catch (Exception e)
		{
			throw new RuntimeException("DeliveriesTBL is empty", e);
		}
	}

}
